<?php
require(dirname(__FILE__) . '/includes/init.php');
if ($_REQUEST['act'] == 'read'){
	$sql = "select add_time from ".$ecs->table('pic_ad')." where ad_id = (select max(ad_id) from ".$ecs->table('pic_ad').")";
    $insert_time = $db->getOne($sql);
	if(gmtime()-$insert_time<=24*3600){
		$dao_str = "今天您已经导入过数据了";
	}else{
		require_once('includes/cls_readexcel.php'); 
		$data= new Spreadsheet_Excel_Reader(); 
		$data->setOutputEncoding('utf-8'); 
		if ($_FILES["file"]["error"] > 0)
		{
			$error = urlencode('文件出错了，错误码：'.$_FILES["file"]["error"]);
			header("Location: orders.php?error=$error\n");exit;
		}else if(end(explode(".",strtolower($_FILES["file"]['name'])))!='xls'){
			$error = urlencode('请选择excel文件');
			header("Location: orders.php?error=$error\n");exit;
		}else{
			$datapath = $_FILES["file"]["tmp_name"];
		}
		$data->read($datapath);
		$count = 0;
		for($i= 2; $i<= $data->sheets[0]['numRows']; $i++){
			$jump_url = "http://www.yihaodian.com/product/".$data->sheets[0]['cells'][$i][3]."_784";
			$sql = "INSERT INTO " .$ecs->table('pic_ad'). " (version,ad_name,page_ids,jump_url,add_time)".
                "VALUES ('".$data->sheets[0]['cells'][$i][1]."', '".$data->sheets[0]['cells'][$i][2]."','".$data->sheets[0]['cells'][$i][4]."','".$jump_url."','".time()."')";
			$db->query($sql);
		}
	}
}
if(!isset($_REQUEST['v'])){
	$version = 1;
}else{
	$version = $_REQUEST['v'];
}
$ad_list = $db->getAll("select ad_id,page_ids,ad_name from ".$ecs->table('pic_ad')." where version='".$version."'");
foreach($ad_list as $index=>$ad){
	$num = $db->getOne("select count(stats_id) as num from ".$ecs->table('stats')." where ad_id='".$ad['ad_id']."'");
	$ad_list[$index]['num'] = $num;
	//print_r($num);
	$visits = 0;
	//$sn_list = $db->getAll("select distinct referer_path from ".$ecs->table('stats')." where ad_id='".$ad['ad_id']."'");
	if($ad['page_ids']){
		$sn_list = explode(',',$ad['page_ids']);
		for($j=0;$j<sizeof($sn_list);$j++){
			$visit = $db->getOne("select count(visit_id) as visit from ".$ecs->table('page_visit')." where products_sn='".$sn[$j]."'");
			$visits += $visit;	
		}
	}
	//echo $visits.'<br>';
	$ad_list[$index]['visits'] = $visits;
	if($visits==0){
		$ad_list[$index]['lv'] = '0'.'%';
	}else{
		$ad_list[$index]['lv'] = number_format($num/$visits*100,2).'%';
	}
	
}

$click = 0;
$visit_num = 0;
$pj_lv = '0.00%';
$total = sizeof($ad_list);
if($total>0){
	for($i=0;$i<$total;$i++){
		$click += $ad_list[$i]['num'];
		$visit_num += $ad_list[$i]['visits'];	
	}
	$click = number_format($click/$total,2);
    $visit_num = number_format($visit_num/$total,2);
 	if($visit_num>0){
		$pj_lv = number_format($click/$visit_num*100,2).'%';
	}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>管理中心 - 广告列表 </title>
<meta name="robots" content="noindex, nofollow">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/general.css" rel="stylesheet" type="text/css" />
<link href="styles/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
</head>
<body>
 
<h1>
<span class="action-span1"><a target="_top" href="index.php?act=main">管理中心</a> </span><span id="search_id" class="action-span1"> - 广告列表 </span>
<div style="clear:both"></div>
</h1>
<?php if(!empty($_GET['error'])){?>
<div class="form-div" id="error">
  <span><a href="#" onclick="javascript:err_hide()" title="关闭"><img src="images/no.gif" border="0" alt="关闭"></a><?php echo urldecode($_GET['error']); ?></span>
</div>
<?php }?>
<div class="form-div">
  <form method="post" action="ads.php?act=read" enctype="multipart/form-data">
   Excel<input type="file" name="file" size="50" />
   <input type="submit" name="readexcel" value="导入" class="button"/>
   <a href="ads.php?v=1" <?php if($version==1){?>style="background: #192E32"<?php }?>>版本1</a>
   <a href="ads.php?v=2" <?php if($version==2){?>style="background: #192E32"<?php }?>>版本2</a>
   <?php if($dao_str){?><span style="color:red;"><?php echo $dao_str;?></span><?php }?>
  </form>
</div>
  <div class="list-div" id="listDiv">
 
<table cellpadding="3" cellspacing="1">
  <tr>
    <th>广告ID</th>
    <th>广告名</th>
	<th>广告地址</th>
	<th>点击次数</th>
	<th>访问次数</th>
	<th>点击率</th>
    <th>操作</th>
  <tr>
  <?php 
    $i = 0;
	if(count($ad_list)>0){
		foreach($ad_list as $ad){
  ?>
   <tr>
    <td><?php echo $ad['ad_id']?></td>
    <td><?php echo $ad['ad_name']?></td>
    <td>http://www.youhi.cn/crm/url.php?aid=<?php echo $ad['ad_id']?></td>
    <td><?php echo $ad['num'];?></td>
    <td><?php echo $ad['visits'];?></td>
	<td><?php echo $ad['lv'];?></td>
	<td><a href="statistics.php?adid=<?php echo $ad['ad_id']?>">查看</a></td>
  <tr>
  <?php 
		}
    }
  ?>
  <tr>
  	<td>平均：</td>
	<td></td>
	<td></td>
	<td><?php echo $click;?></td>
	<td><?php echo $visit_num;?></td>
	<td><?php echo $pj_lv;?></td>
	<td></td>
  </tr>
  </table>
 
<!-- 分页 -->
  </div>
<script type="text/javascript">
function create_url(index){
	var ad_url = $("#default_url_"+index).html();
	$("#ad_url_"+index).html(ad_url+'&pageid='+$("#page_"+index).val());
}
</script>
</body>
</html>